<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/icon.css">
<link rel="stylesheet" href="../static/css/style-app.css" />
<script src="../static/jquery/jquery-1.12.4.js"></script>
<script src="../static/easyui/jquery.easyui.min.js"></script>
<script src="../static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>物资管理</title>
</head>

<body>
	<div class="top-part">
		<div class="header">
			<div class="return">
				<a href="../app/index.do"></a>
			</div>
			<div class="header-title">物资列表</div>
		</div>

	</div>
	<div class="table-container" data-options="region:'center'">
		<div>
			<table id="pro_table" class="easyui-datagrid" style="min-height: 100%">
			</table>
			<div id="pro_tb" class="search-show">
				<div class="search-input">
					<span>项目：</span>
					<span>
						<input style="width: 100%;" id="projectitemid" name="projectitemid" class="easyui-combobox"
							editable="false"
							data-options="
							    valueField: 'id',
							    textField: 'name',
							    url: '../projectitemlist.do?status=2',
							    onLoadSuccess : function(data){ 
								   	if(data.length>0){
								    	$('#projectitemid').combobox('setValue',data[0].id);
							    	}
							    },onSelect: function(rec){
						    		projectitemid = rec.id;
							    	if(rec.projectitemchilds.length>0){
										var url = '../projectitemchildlist?status=2&&projectitemid=' + rec.id;
										$('#projectitemchildid').combobox('reload', url);
										document.getElementById('div1').style.display = '';
							    	}else{
							    		projectitemid = rec.id;
							    		projectitemchildid = 0;
										$('#projectitemchildid').combobox('clear');
										document.getElementById('div1').style.display = 'none';
								    	loadData();
							    	}
							    },panelHeight: 'auto'">
					</span>
				</div>
				<div id="div1" style="display: none;" class="search-input">
					<span>子项目：</span>
					<span>
						<input style="width: 100%;" id="projectitemchildid" name="projectitemchildid" class="easyui-combobox"
							editable="false"
							data-options="
							    valueField: 'id',
							    textField: 'name',
							    loadFilter: function (data) {
							        var obj = {};
							        obj.id = '';
							        obj.name = '全部';
							        data.splice(0, 0, obj);
							        return data;
							    },
							    onSelect: function(rec){
							    	projectitemchildid = rec.id;
							    	loadData();
							    },panelHeight: 'auto'">
					</span>
				</div>
				<div class="search-input">
					<span>状态：</span>
					<span>
						<select id="status" class="easyui-combobox" name="status" style="width: 100%;" editable="false"
							data-options="
							    onSelect: function(rec){
							   		status=rec.value;
							   		if(isnew){
							   			isnew = false;
							   		}else{
							   			loadData();
							   		}
							   		
							    },panelHeight: 'auto'">
							<option value="">全部</option>
							<option value="3">已拒绝</option>
							<option value="4">已审</option>
							<option value="6">待二级审核</option>
						</select>
					</span>
				</div>
				<div class="search-input">
					<span>名称：</span>
					<span><input id="name" class="easyui-textbox" style="width:100%;"/></span>
					
				</div>
				<div class="search-input text-right">
				<a href="#" class="d-btn btn-yellow" id="search">搜索</a>
					<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">添加</a>
				</div>

			</div>
		</div>
	</div>
	<div style="display: none">
		<div id="displayAtt" class="datagrid-toolbar"
			style="width: 98%; max-height: 80%; padding: 5px; top: 30px; text-align: center;"></div>
	</div>
</body>
<script>
	var currentPro;
	var currentChild;
	var status;
	var isnew = true;
	var projectitemid;
	var projectitemchildid;
	var name = "";
	function loadData() {
		if (projectitemchildid > 0){
			$('#pro_table').datagrid({
				rownumbers : true,
				singleSelect : true,
				pagination : true,
				fitColumns : false,
				border : false,
				striped : true,
				toolbar : '#pro_tb',
				url : "../goodss.do",
				method : 'get',
				queryParams : {
					projectitemchildid : projectitemchildid,
					status : status,
					name : name
				},
				onSelect : function(index,row) {
					currentPro = row;
				},
				columns : [ [ {
					field : 'title',
					title : '项目名',
					width : '100%',
					formatter : function(value,row,index) {
						var html='';
						html += '<div class="info-unreviewed">';
						if(row.projectitemchild){
							html+= '<div class="info-item-name" style="width:'+ ellipsis() +'px;margin-bottom:10px;">'+ row.projectitemchild.name +'</div>';
						}else{
							html+= '<div class="info-item-name" style="width:'+ ellipsis() +'px;margin-bottom:10px;">'+ row.item.name +'</div>';
						}
						html+= '<div class="info-item-content" style="width:'+ ellipsis2() +'px;">'+ row.name +'</div>'
						html+= '<div class="info-content" style="width:'+ ellipsis2() +'px;">'+ row.remark +'</div>'
						html+= '<div class="info-num col-3"><span class="c-yellow"><b>初数</b>'+ row.initial +'<span class="info-unit">/'+ row.unit +'</span></span>'
						html +='<span class="c-blue"><b>末数</b>'+ row.ends +'<span class="info-unit">/'+ row.unit +'</span></span>'
						html +='<span class="c-green"><b>实盘</b>'+ nullNum(row.actual) +'<span class="info-unit">/'+ row.unit +'</span></span></div>'
						if (row.status == 4){
							html += '<div class="info-status ok">已审</div>';
						}
						if (row.status == 3){
							html += '<div class="info-status refuse">已拒绝</div>';
						}
						if (row.status == 6){
							html += '<div class="info-status two">待二级审核</div>';
						}
						if (row.status == 6){
							html+= '<div class="edit"><span class="info-edit" onclick="updatepro('+index+')"></span><span class="info-delete" onclick="deletepro('+index+')"></span>'
						}else{
							html+= '<div class="edit"><span class="info-more" onclick="addTab(' + index + ')"></span><span class="info-edit" onclick="updatepro('+index+')"></span>'
						}
						html += '</div></div>'
						return html;
					}
				} ] ]
			});
		}else{
			$('#pro_table').datagrid({
				rownumbers : true,
				singleSelect : true,
				pagination : true,
				fitColumns : false,
				border : false,
				striped : true,
				toolbar : '#pro_tb',
				url : "../goodss.do",
				method : 'get',
				queryParams : {
					projectitemid : projectitemid,
					status : status,
					name : name
				},
				onSelect : function(index,row) {
					currentPro = row;
				},
				columns : [ [ {
					field : 'title',
					title : '项目名',
					width : '',
					formatter : function(value,row,index) {
						var html='';
						html += '<div class="info-unreviewed">';
						if(row.projectitemchild){
							html+= '<div class="info-item-name" style="width:'+ ellipsis() +'px;margin-bottom:10px;">'+ row.projectitemchild.name +'</div>';
						}else{
							html+= '<div class="info-item-name" style="width:'+ ellipsis() +'px;margin-bottom:10px;">'+ row.item.name +'</div>';
						}
						html+= '<div class="info-item-content" style="width:'+ ellipsis2() +'px;">'+ row.name +'</div>'
						html+= '<div class="info-content" style="width:'+ ellipsis2() +'px;">'+ row.remark +'</div>'
						html+= '<div class="info-num col-3"><span class="c-yellow"><b>初数</b>'+ row.initial +'<span class="info-unit">/'+ row.unit +'</span></span>'
						html +='<span class="c-blue"><b>末数</b>'+ row.ends +'<span class="info-unit">/'+ row.unit +'</span></span>'
						html +='<span class="c-green"><b>实盘</b>'+ nullNum(row.actual) +'<span class="info-unit">/'+ row.unit +'</span></span></div>'
						if (row.status == 4){
							html += '<div class="info-status ok">已审</div>';
						}
						if (row.status == 3){
							html += '<div class="info-status refuse">已拒绝</div>';
						}
						if (row.status == 6){
							html += '<div class="info-status two">待二级审核</div>';
						}
							if (row.status == 6){
								html+= '<div class="edit"><span class="info-edit" onclick="updatepro('+index+')"></span><span class="info-delete" onclick="deletepro('+index+')"></span>'
							}else{
								html+= '<div class="edit"><span class="info-more" onclick="addTab(' + index + ')"></span><span class="info-edit" onclick="updatepro('+index+')"></span>'
							}
							html += '</div></div>'
							return html;
							
					}
				} ] ]
			});
		}
	}
	function nullNum(value){
		if(value == null){ 
			return 0;
		}else{
			return value;
		}
	}
	$("#search").click(function() {
		name = $("#name").textbox('getValue');
		loadData();
	});
	function refreshProTable() {
		$('#pro_table').datagrid("reload");
	}
	function newpro() {
		window.location.href = "../app/goodsadd.do";
	}
	function updatepro(index) {
		var item = $("#pro_table").datagrid('getRows')[index];
		window.location.href = "../app/goodsadd.do?id=" + item.id;
	}
	function deletepro(index) {
		currentPro = $("#pro_table").datagrid('getRows')[index];
		if (currentPro){
			if (currentPro.status == 6){
				$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
					if (data){
						$.ajax({
							type : 'DELETE',
							url : "../goodsDelete.do?id=" + currentPro.id,
							contentType : "application/json",
							success : function(data) {
								if (data.success){
									$.messager.show({
										title : '删除',
										msg : '删除成功',
										timeout : 5000,
										showType : 'slide'
									});
									loadData();
								}else{
									$.messager.alert('删除', '删除失败', 'error');
								}
							},
							dataType : "json"
						});
					}
				});
			}else{
				$.messager.alert('警告', '已审核物资不允许删除', 'warning');
			}
		}else{
			$.messager.alert('更新', '请选择要删除的物资', 'warning');
		}
	}
	function displayAtt(index) {
		var row = $('#pro_table').datagrid('getRows')[index];
		var tb = "";
		if(row.status == 6){
			$.each(row.attachments,function(i,val) {
				if(isImage(val.url)){
					tb+= '<img onclick="window.open(\'../'+val.url+ 
					'\',\'_blank\', \' toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no\');" '+
					'src="../'+val.url+'" style="width:100px;"><br><span>'+val.name+'</span><br>';
					tb+= '<a href="javascript:void(0)" onclick="remove('+val.id+')"><span>删除</span></a><br>'
				}else{
					tb+= '<a href="javascript:void(0)" onclick="attachmentFile('+val.id+',\''+val.name+
					'\')"><span>'+val.name+'</span></a><br>';
					tb+= '<a href="javascript:void(0)" onclick="remove('+val.id+')"><span>删除</span></a><br>'
				}
			});
		}else{
			$.each(row.attachments,function(i,val) {
				if(isImage(val.url)){
					tb+= '<img onclick="window.open(\'../'+val.url+ 
					'\',\'_blank\', \' toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no\');" '+
					'src="../'+val.url+'" style="width:100px;"><br><span>'+val.name+'</span><br>';
				}else{
					tb+= '<a href="javascript:void(0)" onclick="attachmentFile('+val.id+',\''+val.name+
					'\')"><span>'+val.name+'</span></a><br>';
				}
			});
		}
		$('#displayAtt').empty().html(tb);
		$('#displayAtt').dialog({
			title : '附件',
			resizable : true,
			closed : false,
			cache : false,
			modal : true
		});
	}
	function attachmentFile(id,name) {
		$.get('../attachmentFile.do?id=' + id, function(data) {
			blob = new Blob([ data ])
			var a = document.createElement('a');
			a.download = name;
			a.href = window.URL.createObjectURL(blob)
			a.click()
		})
	}
	function remove(id) {
		$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
			if (data){
				$.ajax({
					type : 'POST',
					url : "../attachmentDelete.do?id=" + id,
					contentType : "application/json",
					success : function(data) {
						if (data.success){
							$("#att" + id).remove();
							$.messager.alert('删除附件', '删除附件成功', 'success');
						}else{
							$.messager.alert('删除附件', '删除附件失败', 'error');
						}
						window.setInterval("location.reload()", 2000);
					},
					dataType : "json"
				});
			}
		});
	}
	function isImage(url) {
		var index= url.lastIndexOf(".");
		var ext = url.substr(index+1);
		return [ 'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff' ,'ico'].
				indexOf(ext.toLowerCase()) !== -1;
	}
	function addTab(index) {
		var item = $("#pro_table").datagrid('getRows')[index];
		var title = '物资管理-' + item.item.name;
		window.location.href = "../app/goodscurrent.do?goodsid=" + item.id;
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear(),month = date.getMonth() + 1, //月份是从0开始的
		day = date.getDate(),hour = date.getHours(),min = date.getMinutes(),sec = date.getSeconds();
		var newTime = year + '-' + month + '-' + day;
		return newTime;
	}
	function ellipsis(){
		var WIDTH = $("body").width();
		WIDTH-=10;
		var width;
		width = WIDTH-60
		return width;
		/*限制字符数
		if(value.length>num){
			var html1 =new Array("");
			for(var i=0;i<num;i++){
				html1.push(value[i])
			}
			html1.push('...');
			html1 = html1.join("");
			return html1;
		}else{
			return value;
		}*/
	}
	function ellipsis2(){
		var WIDTH = $("body").width();
		var width;
		width = WIDTH-10
		return width;
		/*限制字符数
		if(value.length>num){
			var html1 =new Array("");
			for(var i=0;i<num;i++){
				html1.push(value[i])
			}
			html1.push('...');
			html1 = html1.join("");
			return html1;
		}else{
			return value;
		}*/
	}	
		
</script>
</html>